package com.customization.avic;

import com.alibaba.fastjson.JSONObject;
import com.engine.core.cfg.annotation.CommandDynamicProxy;
import com.engine.core.interceptor.AbstractCommandProxy;
import com.engine.core.interceptor.Command;
import com.engine.workflow.cmd.requestForm.RequestSubmitCmd;
import com.engine.workflow.entity.requestForm.RequestOperationResultBean;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.hrm.User;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;


/**
 * @Classname CustomRequestSubmitCmd
 * @Description TODO()
 * @Author dusongsong
 * @Date 2023-11-09 14:49
 **/


@CommandDynamicProxy(target = RequestSubmitCmd.class, desc = "流程提交后给调用接口")
public class CustomRequestSubmitCmd extends AbstractCommandProxy<Map<String, Object>> {
    @Override
    public Map<String, Object> execute(Command<Map<String, Object>> targetCommand) {
        //获取到被代理对象
        RequestSubmitCmd requestSubmitCmd = (RequestSubmitCmd) targetCommand;
        //获取被代理对象的参数
        Map<String, Object> params = requestSubmitCmd.getParams();
        //参数回写
        requestSubmitCmd.setParams(params);
        //执行标准的业务处理
        Map<String, Object> result = nextExecute(targetCommand);
        //对返回值做加工处理
        RequestOperationResultBean resultBean = (RequestOperationResultBean) result.get("data");
        String executeResult = resultBean.getType().name();
        requestSubmitCmd.writeLog("requestSubmitCmd executeResult======" + executeResult);
        if ("SUCCESS".equals(executeResult)) {
            HttpServletRequest request = requestSubmitCmd.getRequest();
            User user = requestSubmitCmd.getUser();
            String nodeid = (String) params.get("nodeid");
            requestSubmitCmd.writeLog("requestSubmitCmd nodeid======" + nodeid);
            String requestid = (String) params.get("requestid");
            requestSubmitCmd.writeLog("requestSubmitCmd requestid======" + requestid);
            if (Util.getIntValue(requestid) > 0) {
                //推送门户消息
                JSONObject operatorInfo = getOperatorInfo(requestid, nodeid);
                String signorder = operatorInfo.getString("signorder");
                if ("0".equals(signorder)) {
                    requestSubmitCmd.writeLog(">>>>CustomRequestSubmitCmd>>>>>非会签");
                    String userids = operatorInfo.getString("userids");
                    String[] useridArr = userids.split(",");
                    for (String userid : useridArr) {
                        JSONObject DoneResultJson = DoneWfUtil.sendDoneWorkflow(requestid, userid);
                        requestSubmitCmd.writeLog(">>>>>WorkflowMessage>>>>>DoneResultJson=" + DoneResultJson);
                    }
                } else {
                    String userid = user.getUID() + "";
                    requestSubmitCmd.writeLog("requestSubmitCmd user======" + userid);
                    JSONObject DoneResultJson = DoneWfUtil.sendDoneWorkflow(requestid, userid);
                    requestSubmitCmd.writeLog(">>>>>CustomRequestSubmitCmd>>>>>DoneResultJson=" + DoneResultJson);
                }

            }
        }
        return result;
    }


    public static JSONObject getOperatorInfo(String requestid, String nodeid) {
        RecordSet rs = new RecordSet();
        String signorder = "";
        String userids = "";
        JSONObject jsonObject = new JSONObject();
        try {

            rs.executeQuery("select userid,(select t.signorder from workflow_groupdetail t where t.id=groupdetailid) signorder\n" +
                    " from workflow_currentoperator where requestid=? and nodeid=? " +
                    "group by userid,groupdetailid", requestid, nodeid);
            while (rs.next()) {
                String userid = Util.null2String(rs.getString("userid"));
                userids += "".equals(userids) ? userid : "," + userid;
                signorder = Util.null2String(rs.getString("signorder"));
            }
            jsonObject.put("signorder", signorder);
            jsonObject.put("userids", userids);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return jsonObject;
    }
}
